package com.pay.sdk.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

/**
 * Created by Joe on 2017/4/19 0019 17:01.
 * Pull up the wallet interface
 */

public class WalletBrowerUtils {
    private static String TAG = "junfutong";
    private Activity mActivity;
    private String oriUrl = "";
    private String DownLaodAddress = "http://www.jtpay.com/apk/wallet.apk";

    public WalletBrowerUtils(Activity activity) {
        mActivity = activity;
    }

    public void setDownLaodAddress(String downloadAddress) {
        DownLaodAddress = downloadAddress;

    }

    private void openBrower(String url) {
        Uri content_url = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, content_url);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        mActivity.startActivity(Intent.createChooser(intent, "请选择浏览器,建议使用欧朋,搜狗浏览器"));
    }

    @SuppressWarnings("WrongConstant")
    public void choiceBrower(String url) {
        if (mActivity == null) {
            Log.d(TAG, "WalletBroerUtils uninitialized");
            return;
        }
        if (TextUtils.isEmpty(url)) {
            Log.d(TAG, "wallet download url isEmpty,custom not set,then use default");
            url = DownLaodAddress;
        }
        Uri uri = Uri.parse(url.trim());
        Intent urlintent = new Intent(Intent.ACTION_VIEW, uri);
        PackageManager pm = mActivity.getPackageManager(); //get packagemanager
        List<ResolveInfo> queryactivities = pm.queryIntentActivities(urlintent,
                PackageManager.GET_INTENT_FILTERS);
//        Log.d("ResolveInfo", "queryactivities===" + queryactivities.toString());
        for (ResolveInfo info : queryactivities) {
            boolean bFinish = false;
            Log.d("junfutong", info.toString());
            /* oupeng>qihoo>sogou
            * Different phone browsers may have different priorities */
            switch (info.activityInfo.packageName) {
                case "com.oupeng.mini.android":
                case "com.oupeng.browser":
                case "com.opera.browser":
                case "sogou.mobile.explorer":
                    urlintent.setClassName(info.activityInfo.packageName,
                            info.activityInfo.name);
                    bFinish = true;
                    break;
            }
            if (bFinish) {
                break;
            }
            if (queryactivities.indexOf(info) == queryactivities.size() - 1) {
                openBrower(url);
                return;
            }
        }
        urlintent.addCategory(Intent.CATEGORY_DEFAULT);
        urlintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mActivity.startActivity(urlintent);
    }

    /*wap & sdk unify the payment method*/
    public void pullUpWallet(String url) {
        if (oriUrl.equals(url)) {
            Log.d(TAG, "重复发送支付请求,已被过滤");
            return;
        } else {
            oriUrl = url;
        }
        Log.d(TAG, "pullUpWallet=intent=" + url);
        try {
            Intent intent = new Intent("android.intent.action.PAY_WEBVIEW");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("WalletUrl", url);
            mActivity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            choiceBrower(DownLaodAddress);
        }
    }
}
